<template>
  <div class="blog">
    <!-- 博客列表 -->
    <el-table
      :data="blogList"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="id"
        width="180">
      </el-table-column>
      <el-table-column
        prop="title"
        label="标题"
        width="180">
      </el-table-column>
      <el-table-column
        label="封面">
        <template slot-scope="scope">
          <img :src="scope.row.img" alt="">
        </template>
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
           <el-button
           type="primary"
           icon="el-icon-edit"
           circle
           @click="$router.push(`/admin/blogContent/${scope.row.id}`)"
           ></el-button>
           <el-button
           type="danger"
           icon="el-icon-delete"
           circle
           @click="openDelete(scope.row.id)"
           ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.pageIndex"
        :page-sizes="[3, 5, 10]"
        :page-size="query.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getBlogList, deleteBlog } from '@/api/api'
export default {
  name: 'Blog',
  data () {
    return {
      blogList: [],
      query: {
        pageIndex: 1,
        pageSize: 5
      },
      total: 0
    }
  },
  created () {
    this.LoadBlogList()
  },
  methods: {
    async LoadBlogList () {
      const { data: res } = await getBlogList(this.query)
      console.log(res)
      this.blogList = res.data.blogList
      this.total = res.data.total
    },
    openDelete (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await deleteBlog(id)
        if (res.code === 20000) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.LoadBlogList()
        } else {
          this.$message.error(res.error)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSizeChange (val) {
      this.query.pageSize = val
      this.LoadBlogList()
    },
    handleCurrentChange (val) {
      this.query.pageIndex = val
      this.LoadBlogList()
    }
  }
}
</script>
